package com.example.project.utils;

import android.graphics.Color;
import android.util.Log;

import com.example.project.bean.YearDetail;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LineChartManager {
    private LineChart lineChart;
    private YAxis leftAxis;   //左边Y轴
    private YAxis rightAxis;  //右边Y轴
    private XAxis xAxis;      //X轴

    //设置日x轴
    private void setDayXAxis(){
        XAxis xAxis = lineChart.getXAxis(); //获取x轴
        xAxis.setAxisMinimum(0f); //设置最小值
        xAxis.setAxisMaximum(31);  //设置最大值
        xAxis.setLabelCount(15); //设置显示个数
        xAxis.setDrawGridLines(false); //设置是否显示网格线
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //所在的位置
    }
    //设置月x轴
    private void setMonthXAxis(){
        XAxis xAxis = lineChart.getXAxis(); //获取x轴
        xAxis.setAxisMinimum(0f); //设置最小值
        xAxis.setAxisMaximum(12);  //设置最大值
        xAxis.setLabelCount(12); //设置显示个数
        xAxis.setDrawGridLines(false); //设置是否显示网格线
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //所在的位置
    }
    //设置y轴
    private void setYAxis(){
        YAxis axisLeft = lineChart.getAxisLeft(); //获取y轴
//        axisLeft.setAxisMaximum(26); //设置最大值
        axisLeft.setAxisMinimum(0); //设置最小值
        lineChart.getAxisRight().setEnabled(false); //设置折线图右边的轴是否显示
    }

    //启动折线图
    private void initData(List<HashMap<String, Object>> hashMapList, String time) {
        setYAxis();
        if(time.equals("month")){
            setMonthXAxis();
            lineChart.setExtraOffsets(24f,24f,24f,0f); //折线图左，上，右，下的偏移量
            setMonthChartData(hashMapList);
            return;
        }
        setDayXAxis();
        lineChart.setExtraOffsets(24f,24f,24f,0f); //折线图左，上，右，下的偏移量
        setDayChartData(hashMapList);
    }

    //设置日折线图数据
    public void setDayChartData(List<HashMap<String, Object>> hashMapList){
        List<YearDetail> list = new ArrayList<>();
        for(int i=1;i<32;i++){
            boolean isFind = false; //是否都没有
            YearDetail yearDetail=new YearDetail();
            for(int m=0;m<hashMapList.size();m++){
                Map<String,Object> map = hashMapList.get(m);
                if (Integer.parseInt(String.valueOf(map.get("day")))==i){
                    float d=Float.parseFloat(String.valueOf(map.get("money")));
                    yearDetail.setMonth(i);
                    yearDetail.setInmoney(d);
                    list.add(yearDetail);
                    isFind=true;
                }
            }
            if(!isFind){
                yearDetail.setMonth(i);
                yearDetail.setInmoney(0);
                list.add(yearDetail);
            }
        }

        List<Entry> yVals1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            yVals1.add(new Entry(list.get(i).getMonth(),list.get(i).getInmoney()));
        }
        LineDataSet set1=new LineDataSet(yVals1,"折线图");
        set1.setDrawFilled(true);
        set1.setFillColor(Color.WHITE);
        LineData lineData=new LineData(set1);
        lineChart.setData(lineData);
        lineChart.invalidate();
    }

    //设置月折线图数据
    public void setMonthChartData(List<HashMap<String, Object>> hashMapList){
        List<YearDetail> list = new ArrayList<>();
        for(int i=1;i<13;i++){
            boolean isFind = false; //是否都没有
            YearDetail yearDetail=new YearDetail();
            for(int m=0;m<hashMapList.size();m++){
                Map<String,Object> map = hashMapList.get(m);
                if (Integer.parseInt(String.valueOf(map.get("month")))==i){
                    float d=Float.parseFloat(String.valueOf(map.get("money")));
                    yearDetail.setMonth(i);
                    yearDetail.setInmoney(d);
                    list.add(yearDetail);
                    isFind=true;
                }
            }
            if(!isFind){
                yearDetail.setMonth(i);
                yearDetail.setInmoney(0);
                list.add(yearDetail);
            }
        }

        List<Entry> yVals1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            yVals1.add(new Entry(list.get(i).getMonth(),list.get(i).getInmoney()));
        }
        LineDataSet set1=new LineDataSet(yVals1,"折线图");
        set1.setDrawFilled(true);
        set1.setFillColor(Color.WHITE);
        LineData lineData=new LineData(set1);
        lineChart.setData(lineData);
        lineChart.invalidate();
    }
}